﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using Common;

namespace AnesSystem.Controls
{
    /// <summary>
    /// RightTableCtrl.xaml 的交互逻辑
    /// </summary>
    public partial class RightTableCtrl : BasePrintControl
    {
        #region Variables

        private TabControl _cusTabControl;


        #endregion

        #region Constructor
        public RightTableCtrl()
        {
            IsNewCreate = true;
            TableItems = new List<FrameworkElement>();
        }

        #endregion

        #region Properties


        public bool IsNewCreate { get; set; }

        public List<FrameworkElement> TableItems { get; set; }

        public TabControl CusTableControl
        {
            get
            {
                return _cusTabControl;
            }
            set
            {
                _cusTabControl = value;
            }
        }


        public string HeaderDisplayContent { get; set; }




        public UIElement CusContent
        {
            get
            {
                return this.Content as UIElement;
            }
            set
            {
                if (value is TabControl)
                {
                    Content = value;
                    _cusTabControl = (TabControl)value;
                }
                else
                {
                    _cusTabControl = null;
                    Content = CreateItem(value);
                }
            }
        }

        public virtual ViewModels.ViewModel ViewModel
        {
            get
            {
                return this.DataContext as ViewModels.ViewModel;
            }
        }

        private FrameworkElement CreateItem(UIElement item)
        {
            Grid grid = new Grid();
            RowDefinition r1 = new RowDefinition();
            r1.Height = GridLength.Auto;
            RowDefinition r2 = new RowDefinition();
            grid.RowDefinitions.Add(r1);
            grid.RowDefinitions.Add(r2);
            Type HeaderType = Type.GetType(DataManager.Instance.CurrentHospital.TitleNameSpace);
            if (HeaderType != null)
            {
                BaseTableTitleCtrl header = Activator.CreateInstance(HeaderType) as BaseTableTitleCtrl;
                if (header != null)
                {
                    if (string.IsNullOrEmpty(HeaderDisplayContent))
                    {
                        header.Titile = DataManager.Instance.CurrentHospital.DisplayHospitalName.ToString();
                    }
                    else
                    {
                        header.Titile = HeaderDisplayContent;
                    }
                    Grid.SetRow(header, 0);
                    grid.Children.Add(header);
                }
                else
                {
                    Common.LogHelper.Error(this.GetType(), "表单标题控件未生成");
                }
            }
            Grid.SetRow(item, 1);
            grid.Children.Add(item);
            Thickness margin = new Thickness(50,20,40,30);
            if (HospitalConfig.Instance.CurrentHospital.HospitalCode.Equals(HospitalConfig.HOSPITAL_CODE_CQ_FYBJY))
            {
                margin = new Thickness(50);
            }
            grid.Margin = margin;
            grid.HorizontalAlignment = HorizontalAlignment.Center;
            return grid;
        }

        #endregion

        #region 依赖属性

        #endregion

        #region Event

        #endregion

        #region Private Methods

        #endregion

        #region Public/Protected Methods

        public override void PrintVisual()
        {
            if (_cusTabControl != null)
            {
                Print(_cusTabControl.SelectedContent as PrintTemplate);
            }
            else
            {
                Print(this);
            }
        }

        #endregion

        #region EventHandlers

        #endregion
    }
}
